package com.ttc.group2.videochat.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DbHelper {

	private final Context mContext;
	private static final String TAG = DbHelper.class.getSimpleName();
	private static final String DATABASE_NAME = "videochat.db";
	private static final int DATABASE_VERSION = 1;
	private static DbHelper instance;
	private SQLiteDatabase mDb;
	private DatabaseHelper mDbHelper;

	private static class DatabaseHelper extends SQLiteOpenHelper {

		public DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			Tables.People.setup(db);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
					+ newVersion);
			// TO DO something here

		}

	}

	private DbHelper(Context context) {
		this.mContext = context;
	}

	public static synchronized DbHelper getInstance(Context context) {
		if (instance == null) {
			instance = new DbHelper(context);
			instance.open();
		}
		return instance;
	}

	public synchronized void open() {
		this.mDbHelper = new DatabaseHelper(mContext);
		mDb = this.mDbHelper.getWritableDatabase();
	}

	public synchronized void close() {
		mDbHelper.close();
	}

}
